﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_Products : System.Web.UI.UserControl
{
    static DataTable tbGioHang = new DataTable();
    BUS_Product product = new BUS_Product();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Trang sản phẩm";
        if (!IsPostBack)
        {
            
            if (Session["GioHang"] != null)
            {
                tbGioHang = Session["GioHang"] as DataTable;
            }
            else
            {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("idSP", typeof(int));
                tbGioHang.Columns.Add("TenSP", typeof(string));
                tbGioHang.Columns.Add("Gia", typeof(double));
                tbGioHang.Columns.Add("SoLuong", typeof(int));
                tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
            }
            LoadData();
        }
    }
    private void LoadData()
    {

        lvProductNew.DataSource = product.Product_GetByTop(0, "[Status] = 1", "[Date] DESC, [Order]");
            lvProductNew.DataBind();

            //rptProductHot.DataSource = product.Product_GetByTop(8, "", "[View] DESC, [Order]");
            //rptProductHot.DataBind();
            //rptSanPhanBanChay.DataSource = product.Product_GetByTop(8, "", "[Buy] DESC, [Order]");
            //rptSanPhanBanChay.DataBind();
        
         
    }

    
    protected void lvProductNew_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "MuaHang")
        {
            if (Session["GioHang"] != null)
            {
                tbGioHang = Session["GioHang"] as DataTable;
            }
            else
            {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("idSP", typeof(int));
                tbGioHang.Columns.Add("TenSP", typeof(string));
                tbGioHang.Columns.Add("Gia", typeof(double));
                tbGioHang.Columns.Add("SoLuong", typeof(int));
                tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
            }
            int IdSp = int.Parse(e.CommandArgument.ToString());

            string strTenSP = ((Label)e.Item.FindControl("lblTenSanPham")).Text;
            float flGia = float.Parse(((Label)e.Item.FindControl("lblGia")).Text);
            int intSoLuong = 1;
            foreach (DataRow row in tbGioHang.Rows)
            {//Kiem tr neu mat hang da co roi thi tang so luong len 1
                if ((int)row["idSP"] == IdSp)
                {
                    row["SoLuong"] = (int)row["SoLuong"] + 1;
                    goto GioHang;
                }
            }
            tbGioHang.Rows.Add(IdSp, strTenSP, flGia, intSoLuong);
        GioHang:
            Session["GioHang"] = tbGioHang;


            Response.Redirect(Request.RawUrl);
        }
    }
}